package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.PickAddr;
import com.powernode.model.Result;
import com.powernode.service.PickAddrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @create: 2024/7/19
 * @author: gyhe10
 * @description: 自提点管理层
 **/

@Api(tags = "自提点接口管理")
@RestController
@RequestMapping("shop/pickAddr")
public class PickAddrController {

    @Resource
    private PickAddrService pickAddrService;

    /**
     * 多条件分页查询自提点列表
     * @param current 页码
     * @param size 每页记录数
     * @param addrName 地址名称
     * @return
     */
    @ApiOperation("多条件分页查询自提点列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('shop:pickAddr:page')")
    public Result<Page<PickAddr>> loadPickAddrPage(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   @RequestParam(required = false) String addrName) {
        Page<PickAddr> page = new Page<>(current, size);
        page = pickAddrService.page(page, new LambdaQueryWrapper<PickAddr>()
                .like(StringUtils.hasText(addrName), PickAddr::getAddrName, addrName)
                .orderByDesc(PickAddr::getProvinceId));
        return Result.success(page);
    }

    /**
     * 新增自提点
     * @param pickAddr 自提点对象
     * @return
     */
    @ApiOperation("新增自提点")
    @PostMapping
    @PreAuthorize("hasAuthority('shop:pickAddr:save')")
    public Result<String> savePickAddr(@RequestBody PickAddr pickAddr) {
        Boolean saved = pickAddrService.savePickAddr(pickAddr);
        return Result.handle(saved);
    }

    /**
     * 根据ID查询自提点详情
     * @param addrId 自提点ID
     * @return
     */
    @ApiOperation("根据ID查询自提点详情")
    @GetMapping("info/{addrId}")
    @PreAuthorize("hasAuthority('shop:pickAddr:info')")
    public Result<PickAddr> loadPickAddrInfo(@PathVariable Long addrId) {
        PickAddr pickAddr = pickAddrService.getById(addrId);
        return Result.success(pickAddr);
    }

    /**
     * 修改自提点
     * @param pickAddr 自提点对象
     * @return
     */
    @ApiOperation("修改自提点")
    @PutMapping
    @PreAuthorize("hasAuthority('shop:pickAddr:update')")
    public Result<String> modifyPickAddr(@RequestBody PickAddr pickAddr) {
        boolean modified = pickAddrService.updateById(pickAddr);
        return Result.handle(modified);
    }

    /**
     * 单个/批量删除自提点
     * @param addrIds 自提点ID集合
     * @return
     */
    @ApiOperation("单个/批量删除自提点")
    @DeleteMapping
    @PreAuthorize("hasAuthority('shop:pickAddr:delete')")
    public Result<String> removePickAddr(@RequestBody List<Long> addrIds) {
        boolean removed = pickAddrService.removeBatchByIds(addrIds);
        return Result.handle(removed);
    }



}
